﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Controllers;
using Way.Bus.Wms.Model.BaseData;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;
using Way.EAP.DataAccess.Regulation;
using Way.Contract.MVC;
using Way.Bus.Model.Purview;
using Way.Contract.Data.ControllerData;
using Way.EAP.DataAccess.Data;
using Way.EAP;
using Way.Contract.Data;

namespace Way.Bus.Wms.Controllers.BaseData
{
    public class WmsRoleDataController : RoleDataController
    {
        public WmsRoleDataController() : base(new WmsRoleData()) { }
        protected override void OnInitViewChildItem(ModelData data, ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (item.IsField("MODELID"))
                item.visible = false;
        }
        public static void SearchManage(ControllerBase cb, List<SQLWhere> where,loadItem item)
        {
            IEntityBase ieb;
            if (item.selectTag == null)
            {
                if (string.IsNullOrEmpty(item.selecttype))
                    ieb = cb.model as IEntityBase;
                else
                    ieb =Common.CreateInstance(Common.GetType(item.selecttype)) as IEntityBase;
            }
            else
                ieb = item.selectTag as IEntityBase;
            if (ieb != null)
            {
                EntityList<WmsRoleData> list = new EntityList<WmsRoleData>(cb.model.GetDataAction());
                list.GetData();
                OfficeWorker ow = new OfficeWorker();
                ow.ID = int.Parse(cb.user.ID);
                IDataAction action = cb.model.GetDataAction();
                ow.Roles.DataAction = action;
                ow.Roles.GetData();
                List<WmsRoleData> rolelist = new List<WmsRoleData>();
                foreach (UserRole r in ow.Roles)
                {
                    WmsRoleData rd = list.Find(delegate(WmsRoleData d) { return d.RoleID.Equals(r.RoleID); });
                    if (rd != null)
                        rolelist.Add(rd);
                }
                if (rolelist.Count > 0)
                {
                    foreach (WmsRoleData wrd in rolelist)
                    {
                        IHierarchicalEntityView[] vss = ((IEntityBase)wrd).GetChildEntityList();
                        foreach (IHierarchicalEntityView v in vss)
                        {
                            Dictionary<string, EntityForeignKeyAttribute> fk = ((IEntityBase)v.ChildEntity).GetForeignKey();
                            foreach (KeyValuePair<string, EntityForeignKeyAttribute> kv in fk)
                            {
                                if (kv.Value.FieldType == ieb.GetType())
                                {
                                    IEntityList iel = v.GetEntityList();
                                    iel.DataAction = action;
                                    iel.GetData();
                                    string w = "";
                                    foreach (EntityBase eb in iel)
                                    {
                                        if (string.IsNullOrEmpty(w))
                                            w = eb[kv.Key].ToString();
                                        else
                                            w += "," + eb[kv.Key].ToString();
                                    }
                                    if (w == "")
                                        w = "0";
                                    SQLWhere w1 = new SQLWhere();
                                    w1.ColumnName = "ID";
                                    w1.Symbol = "in";
                                    w1.Value = "(" + w + ")";
                                    where.Add(w1);
                                    break;
                                }
                                foreach (KeyValuePair<string, EntityForeignKeyAttribute> kve in ieb.GetForeignKey())
                                {
                                    if (kv.Value.FieldType == kve.Value.FieldType)
                                    {
                                        //借货单中应限制发货单位，不应限制借货单位 add by fisher 2015-01-19
                                        if (kve.Key == "ConductioCompany")
                                        {
                                            continue; 
                                        } 
                                        IEntityList iel = v.GetEntityList();
                                        iel.DataAction = action;
                                        iel.GetData();
                                        string w = "";
                                        foreach (EntityBase eb in iel)
                                        {
                                            if (string.IsNullOrEmpty(w))
                                                w = eb[kv.Key].ToString();
                                            else
                                                w += "," + eb[kv.Key].ToString();
                                        }
                                        if (w == "")
                                            w = "0";
                                        SQLWhere w1 = new SQLWhere();
                                        w1.ColumnName = kve.Key;
                                        w1.Symbol = "in";
                                        w1.Value = "(" + w + ")";
                                        where.Add(w1);
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }
    }
}
